package com.moon.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moon.common.model.entity.UserInterfaceInfo;
import com.moon.project.common.ErrorCode;
import com.moon.project.exception.BusinessException;
import com.moon.project.mapper.UserInterfaceInfoMapper;
import com.moon.project.service.UserInterfaceInfoService;
import org.springframework.stereotype.Service;

/**
* @author chenliang
* @description 针对表【user_interface_info(用户调用接口关系表)】的数据库操作Service实现
* @createDate 2024-10-14 14:10:33
*/
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
    implements UserInterfaceInfoService{

    /**
     * 校验
     *
     * @param userInterfaceInfo
     * @param add               是否为创建校验
     */
    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long interfaceInfoId = userInterfaceInfo.getInterfaceInfoId();
        Long userId = userInterfaceInfo.getUserId();
        Integer leftNum = userInterfaceInfo.getLeftNum();

        //修改校验，设置校验规则
        if (add) {
            if (interfaceInfoId == null || userId == null) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口或用户不存在");
            }
        }
        if (leftNum<=0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口剩余调用次数不足！！！");
        }
    }

    /**
     * 接口调用次数统计,调用次数+1
     *
     * @param userId
     * @param interfaceInfoId
     * @return
     */
    @Override
    public boolean invokeInterfaceInfoCount(long userId, long interfaceInfoId) {
        //校验请求参数
        if (userId <= 0 || interfaceInfoId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //查询用户接口信息
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userId", userId);
        updateWrapper.eq("interfaceInfoId", interfaceInfoId);
        updateWrapper.gt("leftNum", 0);
        //接口调用成功后，剩余接口调用次数-1，总接口调用次数+1
        //todo 这里可能会出现接口大量被调用的情况，应该使用事务和分布式锁来实现数据的一致性
        updateWrapper.setSql("leftNum = leftNum -1, totalNum = totalNum + 1");
        boolean isUpdate = this.update(updateWrapper);
        return isUpdate;
    }

    /**
     * 获取调用接口信息
     *
     * @param userId
     * @param interfaceInfoId
     * @return
     */
    @Override
    public UserInterfaceInfo getUserInterfaceInfo(long userId, long interfaceInfoId) {
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("interfaceInfoId", interfaceInfoId);
        UserInterfaceInfo userInterfaceInfo = this.getOne(queryWrapper);
        return userInterfaceInfo;
    }

}




